package org.squabble.web.admin;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.squabble.SquabbleConstants;
import org.squabble.domain.Account;
import org.squabble.service.AccountService;

@Controller
@RequestMapping("/admin/account/edit")
public class AccountEditController {

	@Autowired
	AccountService accountService;

	@RequestMapping(method=RequestMethod.GET)
	public String editSetupForm(
			@RequestParam(value = "id", required = false) Long id,
			ModelMap model) {
		if (id == null) {
			model.addAttribute(SquabbleConstants.POST_PROCESS_MESSAGE,
					"admin.account.edit.processed[0]");
			return "redirect:/admin/user";
		}
		Account account = accountService.getAccount(id);
		if (account == null) {
			model.addAttribute(SquabbleConstants.POST_PROCESS_MESSAGE,
					"admin.account.edit.processed[1]");
			return "redirect:/admin/user";
		}
		return "admin.user.edit";
	}

	@RequestMapping(method=RequestMethod.POST)
	public String processSubmit(ModelMap model, BindingResult result) {
		model.addAttribute(SquabbleConstants.POST_PROCESS_MESSAGE,
				"admin.account.edit.processed[2]");
		return "redirect:/admin/user";
	}

}
